﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace nsWeldController
{
    public partial class DockBatteryBox : DockContent
    {
        public DockBatteryBox()
        {
            InitializeComponent();
        }

        private CLog mLog = CWeldController.mLog;
        public BindingList<CCoordinate> mBatteryBoxList = new BindingList<CCoordinate>();

        private void DockBatteryBox_Load(object sender, EventArgs e)
        {
            mBatteryBoxList.AllowNew = true;
            mBatteryBoxList.AllowRemove = true;
            mBatteryBoxList.AllowEdit = true;
            dgvBatteryBoxList.AutoGenerateColumns = true;
            dgvBatteryBoxList.AllowUserToAddRows = false;
            dgvBatteryBoxList.DataSource = mBatteryBoxList;
        }

        /// <summary>
        /// 重写以float方式显示的dock窗体的show方法
        /// 区分是否第一次显示，DefaultFloatWindowSize只在第一次显示的时候有效，窗口hide后再显示时候不再指定显示类型
        /// 这样多个float的大小设置不会相互影响。
        /// </summary>
        private bool isFirstShow = true;
        new public void Show(DockPanel dockPanel)
        {
            if (isFirstShow)
            {
                dockPanel.DefaultFloatWindowSize = new Size(this.Size.Width, this.Size.Height);
                base.Show(dockPanel, DockState.Float);
                isFirstShow = false;
            }
            else
            {
                base.Show(dockPanel);
            }
        }

        private void btnBatteryBoxInit_Click(object sender, EventArgs e)
        {
            int batteryBoxRows = 0;
            int batteryBoxColumns = 0;
            int.TryParse(txtBatteryBoxRows.Text, out batteryBoxRows);
            int.TryParse(txtBatteryBoxColumns.Text, out batteryBoxColumns);
            if (batteryBoxRows <= 0 || batteryBoxColumns <= 0)
            {
                mLog.LogMessage("请输入正确的行列数\n");
                return;
            }

            int leftTopBatteryX = 0;
            int leftTopBatteryY = 0;
            int rightBottomBatteryX = 0;
            int rightBottomBatteryY = 0;
            int.TryParse(txtLeftTopX.Text, out leftTopBatteryX);
            int.TryParse(txtLeftTopY.Text, out leftTopBatteryY);
            int.TryParse(txtRightBottomX.Text, out rightBottomBatteryX);
            int.TryParse(txtRightBottomY.Text, out rightBottomBatteryY);

            float leftTopX = 0;
            float leftTopY = 0;
            float rightBottomX = 0;
            float rightBottomY = 0;
            if (batteryBoxColumns == 1)
            {
                if (leftTopBatteryX != rightBottomBatteryX)
                {
                    mLog.LogMessage("列数为1，电池X轴坐标应相同，请确认\n");
                    return;
                }
                leftTopX = 0;
                rightBottomX = leftTopBatteryX * 2;
            }
            else
            {
                if (leftTopBatteryX >= rightBottomBatteryX)
                {
                    mLog.LogMessage("右下角电池X坐标必须大于左上角电池\n");
                    return;
                }
                leftTopX = leftTopBatteryX - (float)(rightBottomBatteryX - leftTopBatteryX) / (float)(batteryBoxColumns - 1) / 2;
                rightBottomX = rightBottomBatteryX + (float)(rightBottomBatteryX - leftTopBatteryX) / (float)(batteryBoxColumns - 1) / 2;                
            }

            if (batteryBoxRows == 1)
            {
                if (leftTopBatteryY != rightBottomBatteryY)
                {
                    mLog.LogMessage("行数为1，电池Y轴坐标应相同，请确认\n");
                    return;
                }
                leftTopY = 0;
                rightBottomY = leftTopBatteryY * 2;
            } 
            else
            {
                if (leftTopBatteryY >= rightBottomBatteryY)
                {
                    mLog.LogMessage("右下角电池Y轴坐标必须大于左上角电池\n");
                    return;
                }
                leftTopY = leftTopBatteryY - (float)(rightBottomBatteryY - leftTopBatteryY) / (float)(batteryBoxRows - 1) / 2;
                rightBottomY = rightBottomBatteryY + (float)(rightBottomBatteryY - leftTopBatteryY) / (float)(batteryBoxRows - 1) / 2;
            }
            
            batteryBoxCtrl1.boxOrigin = new PointF(leftTopX, leftTopY);
            batteryBoxCtrl1.boxWidth = rightBottomX - leftTopX;
            batteryBoxCtrl1.boxHeight = rightBottomY - leftTopY;
            batteryBoxCtrl1.InitializeBox(batteryBoxColumns, batteryBoxRows);
        }

        private void dgvBatteryBoxList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, new SolidBrush(Color.CadetBlue), e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 5);
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加行号时发生错误，错误信息：" + ex.Message, "操作失败");
            }
        }

        private void btnAddToBatteryList_Click(object sender, EventArgs e)
        {
            foreach (PointF pointF in batteryBoxCtrl1.batteryList)
            {
                CCoordinate coordinate = new CCoordinate();
                int z = 0;
                int.TryParse(txtZCoor.Text, out z);
                coordinate.x = (int)pointF.X;
                coordinate.y = (int)pointF.Y;
                coordinate.z = z;
                mBatteryBoxList.Add(coordinate);
            }
            if (dgvBatteryBoxList.Rows.Count > 0)
            {
                dgvBatteryBoxList.CurrentCell = dgvBatteryBoxList.Rows[dgvBatteryBoxList.Rows.Count - 1].Cells[0];
            }
        }

        private void btnSendBatteryBoxInfo_Click(object sender, EventArgs e)
        {
            int count = mBatteryBoxList.Count;
            if (count == 0)
            {
                if (MessageBox.Show("电池参数为空，是否删除控制板端的原有记录？", "提醒", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }
            }
            int structLen = System.Runtime.InteropServices.Marshal.SizeOf(typeof(ET_Coordinate));
            byte[] bytes = new byte[structLen * count];
            for (int i = 0; i < count; i++)
            {
                ET_Coordinate batteryCoor = new ET_Coordinate();
                batteryCoor.x = mBatteryBoxList[i].x;
                batteryCoor.y = mBatteryBoxList[i].y;
                batteryCoor.z = mBatteryBoxList[i].z;
                Buffer.BlockCopy(CWeldController.StructToBytes(batteryCoor), 0, bytes, i * structLen, structLen);
            }

            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_BATTERYLIST;
            stSend.sendMsg = new byte[bytes.Length + 4];
            stSend.sendMsg[0] = (byte)(count & 0xff);
            stSend.sendMsg[1] = (byte)(count >> 8 & 0xff);
            stSend.sendMsg[2] = (byte)(count >> 16 & 0xff);
            stSend.sendMsg[3] = (byte)(count >> 24 & 0xff);
            
            Buffer.BlockCopy(bytes, 0, stSend.sendMsg, 4, bytes.Length);
            EnQueueSend(stSend);
        }

        private void EnQueueSend(ET_GPES_SENDMSG stSend)
        {
            CWeldController.mDockSerialProtocol.EnQueueSend(stSend);
        }
    }
}
